package com.atguigu.jxc.controller;

import com.alibaba.fastjson.JSONArray;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.DamageList;
import com.atguigu.jxc.entity.DamageListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.DamageListGoodsService;
import com.atguigu.jxc.service.UserService;
import com.atguigu.jxc.util.UserUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author tongatong
 * @name DamageListGoodsController
 * @description
 * @date 2021/10/15
 * @time 19:35
 */
@RestController
@RequestMapping("/damageListGoods")
public class DamageListGoodsController {
    @Resource
    private DamageListGoodsService damageListGoodsService;
    @Resource
    private UserService userService;

    @PostMapping("/save")
    public ServiceVO<Void> save(HttpSession session, DamageList damageList, String damageListGoodsStr, String damageNumber) {
        /*System.out.println("-----------------------");
//        DamageList(damageListId=null, damageNumber=BS1634298845031, damageDate=2021-10-15,
//        remarks=111, userId=null, trueName=null)
        System.out.println(damageList);
        *//*[{"goodsId":1,"goodsTypeId":10,"goodsCode":"0001","goodsName":"陶华碧老干妈香辣脆油辣椒",
        "goodsModel":"红色装","goodsUnit":"瓶","lastPurchasingPrice":8.5,"price":"8.5",
        "goodsNum":"2","total":17}]*//*
        System.out.println(damageListGoodsStr);
        //JSON字符串转list
        List<DamageListGoods> damageListGoods = JSONArray.parseArray(damageListGoodsStr, DamageListGoods.class);
        damageListGoods.forEach(System.out::println);
        //BS1634298845031
        System.out.println(damageNumber);
        System.out.println("-----------------------");*/

        damageList = UserUtil.setUserInfoForObject(session, damageList);

        //JSON字符串转list
        List<DamageListGoods> damageListGoodsList = JSONArray.parseArray(damageListGoodsStr, DamageListGoods.class);
        damageListGoodsService.save(damageList, damageListGoodsList, damageNumber);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    /*private DamageList setUserInfoForDamageList(HttpSession session, DamageList damageList) {
        User user = (User)session.getAttribute("currentUser");
//        System.out.println(user);
        damageList.setTrueName(user.getTrueName());
        damageList.setUserId(user.getUserId());

        return damageList;
    }*/

    @PostMapping("/list")
    public Map<String,Object> list(String sTime, String eTime, HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        User user = new User();
        user = UserUtil.setUserInfoForObject(session, user);
        List<DamageList> damageListList = damageListGoodsService.list(sTime, eTime, user);
        result.put("rows", damageListList);
        return result;
    }
}
